package com.lcf.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.lcf.framework.utils.UserUtils;
import com.lcf.framework.vo.PageVO;
import com.lcf.system.dao.GoodsCommentDao;
import com.lcf.system.dto.GoodsCommentDTO;
import com.lcf.system.entity.GoodsComment;
import com.lcf.system.query.GoodsCommentPageQuery;
import com.lcf.system.service.GoodsCommentService;
import com.lcf.system.vo.GoodsCommentVO;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 商品评价(GoodsComment)表服务实现类
 *
 * @author lcf
 * @since 2025-04-24 21:14:32
 */
@Service("goodsCommentService")
public class GoodsCommentServiceImpl extends ServiceImpl<GoodsCommentDao, GoodsComment> implements GoodsCommentService {

    @Override
    public boolean insert(GoodsCommentDTO dto) {
        return BeanUtil.copyProperties(dto, GoodsComment.class).insert();
    }

    @Override
    public boolean update(Long id, GoodsCommentDTO dto) {
        GoodsComment goodsComment = BeanUtil.copyProperties(dto, GoodsComment.class);
        goodsComment.setId(id);
        goodsComment.setOpenId(UserUtils.getOpenId());
        goodsComment.setCommentTime(LocalDateTime.now());

        return goodsComment.updateById();
    }

    @Override
    public boolean delete(Long id) {
        return removeById(id);
    }

    @Override
    public GoodsCommentVO query(Long id) {
        GoodsComment goodsComment = getById(id);
        return BeanUtil.copyProperties(goodsComment, GoodsCommentVO.class);
    }

    @Override
    public PageVO<GoodsCommentVO> page(GoodsCommentPageQuery query) {
        Page<GoodsComment> page = Db.lambdaQuery(GoodsComment.class)
                .eq(GoodsComment::getGoodsId, query.getGoodsId())
                .orderByDesc(GoodsComment::getCommentTime)
                .page(query.toPage());

        return new PageVO<>(page, GoodsCommentVO.class);
    }
}

